package org.dydl.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dydl.common.dto.ResponseData;
import org.dydl.common.dto.ResponseMessage;
import org.dydl.dao.system.repository.IAreaListRepository;
import org.dydl.dao.system.repository.IAreaProductListRepository;
import org.dydl.dao.system.repository.IAreaRepository;
import org.dydl.dao.system.repository.IBusinessListRepository;
import org.dydl.model.system.Area;
import org.dydl.model.system.AreaList;
import org.dydl.model.system.AreaProductList;
import org.dydl.model.system.BusinessList;
import org.dydl.model.system.Orders;
import org.dydl.model.system.vo.ExportVO;
import org.dydl.model.system.vo.SafeConfess;
import org.dydl.model.system.vo.SafeConfessContent;
import org.dydl.service.system.IBusinessListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**  
 * @ClassName: BusinessListServiceImpl  
 * @Description: TODO(这里用一句话描述这个类的作用)  
 * @author 55436  
 * @date 2019年12月19日    
 */
@Service
public class BusinessListServiceImpl implements IBusinessListService {
	
	@Autowired
	private IBusinessListRepository businessListRepository;
	
	@Autowired
	private IAreaRepository areaRepository;
	@Autowired
	private IAreaListRepository areaListRepository;
	@Autowired
	private IAreaProductListRepository areaProductListRepository;
	
	private ResponseMessage res = new ResponseMessage();

	
	@Override
	public ResponseMessage list(int page, int rows, String name) {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<BusinessList> pageList = null;
		if (StringUtils.isNotBlank(name)) {
			pageList = businessListRepository.getBusinessListLike(String.format("%s%s%s", "%", StringUtils.trim(name), "%"),pageable);
		} else {
			pageList = businessListRepository.getBusinessList(pageable);
		}
		return new ResponseMessage(true, new ResponseData<BusinessList>(page, pageList.getTotalPages(),
				((int) pageList.getTotalElements()), pageList.getContent()));
	}


	/* (non-Javadoc)
	 * @see org.yorma.service.system.IBusinessListService#detailList(java.lang.String)
	 */
	@Override
	public List<Area> detailList(String orderNo) {
		// TODO Auto-generated method stub
		return null;
	}


	/* (non-Javadoc)
	 * @see org.yorma.service.system.IBusinessListService#findByName(java.lang.String)
	 */
	@Override
	public ResponseMessage findBybusinessIds(String businessId) {
		try {
			BusinessList orders =  businessListRepository.findBybusinessIds(businessId);
			res.setSuccess(true);
			res.setData(orders);
		} catch (Exception e) {
			res.setSuccess(false);
			res.setMsg(e.getMessage());
		}
		return res;
	}


	/* (non-Javadoc)
	 * @see org.yorma.service.system.IBusinessListService#findAllName()
	 */
	@Override
	public List<Area> findAllName() {
		
		List<Area> list = areaRepository.findAllName();
		
		return list;
	}


	@Override
	public ResponseMessage save(BusinessList bl) {
		
		try {
			businessListRepository.save(bl);
			res.setSuccess(true);
			
		} catch (Exception e) {
			res.setSuccess(false);
			res.setMsg(e.getMessage());
		}
		
		return res;
	}


	@Override
	public List<ExportVO> getExportList(String businessId) {
		// TODO Auto-generated method stub
		ExportVO exportVO;
		List<ExportVO> exportList = new ArrayList<ExportVO>();
		List<AreaList> areaList = areaListRepository.getAreaList(businessId);
		List<AreaProductList> areaProductList = areaProductListRepository.findByBusinessId(businessId);
//		for (ExportVO exportVO : exportList) {
			for (AreaList areaList2 : areaList) {
				for (AreaProductList areaProductList2 : areaProductList) {
					exportVO = new ExportVO();
					exportVO.setAreaName(areaList2.getName());
					exportVO.setProductName(areaProductList2.getProductName());
					exportVO.setNumb(areaProductList2.getNumb()+"");
					exportVO.setModel(areaProductList2.getModel());
					exportVO.setDescribes(areaProductList2.getDescribes());
					exportVO.setPrice(areaProductList2.getFloorPrice()+"");
					exportVO.setRetailPrice(areaProductList2.getRetailPrice()+"");
					exportVO.setRemarks(areaProductList2.getRemarks());
					exportList.add(exportVO);
				}
			}
//		}
		return exportList;
	}

}
